package com.ymatou.iapi.sellerproduct.service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.ymt.core.tool.Logger;
import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.business.service.AssertService;
import com.ymttest.database.model.AppCountry;
import com.ymttest.database.mongo.LiveBasicInfoWapper;
import com.ymttest.database.mongo.LiveModelWapper;
import com.ymttest.database.mongo.LiveProductInfoWapper;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.sqlwapper.AppCountryWapper;
import com.ymttest.database.sqlwapper.YmtC2CLiveActivityWapper;
import com.ymttest.database.sqlwapper.YmtProductBrandWapper;
import com.ymttest.database.sqlwapper.YmtProductCategoryWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.database.sqlwapper.YmtProductsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class AddProductInLivesCall extends YmatouAutoTestCall{
	private static String opurl ="/api/Product/AddProductInLives";
	
	public AddProductInLivesCall() {
		super(EnvSetup.getSystemUrl("sellerproduct_iapi"),opurl,"POST","JSON");
	}
	@Override
	public void addDefHeader() {
		//client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	/**
	 * JSONObject liveRqBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
	 * JSONObject productRqBean=SellerProductCallService.addNewProductOnSale(sellerId,sellerName,map); 
	 * 
	 * 添加商品到直播 - 检查【老Mongo库】【LiveProductInfo】
	 * @param liveRqBean 直播请求+返回报文
	 * @param productRqBean 发布商品请求+返回报文
	 * @param liveId 直播id	
	 * @param productId 商品id
	 * @throws Exception
	 */
	public static void checkLiveProductInfo (JSONObject liveRqBean,JSONObject productRqBean,int liveId,String productId) throws Exception{
		JSONObject liveBean = liveRqBean.getJSONObject("JsonBeanRequest").getJSONObject("ActivityRequest");
		JSONObject productBean = productRqBean.getJSONObject("JsonBeanRequest").getJSONObject("Product");
		
		
		//category data
		YmtProductCategoryWapper ymtProductCategoryWapper = new YmtProductCategoryWapper();
		
		int ThirdCategoryId = Integer.parseInt(productBean.get("iThirdCategoryId").toString());
		
		Map<String, Object> ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(ThirdCategoryId).get(0);
		String ThirdCategoryName = ret.get("sCategory").toString();
		int CategoryId = Integer.parseInt(ret.get("iMasterCategory").toString());
		
		ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(CategoryId).get(0);
		String CategoryName = ret.get("sCategory").toString();
		int MasterCategoryId = Integer.parseInt(ret.get("iMasterCategory").toString());
		
		ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(MasterCategoryId).get(0);
		String MasterCategoryName = ret.get("sCategory").toString();
		
		
		//Brand
		YmtProductBrandWapper ymtProductBrandWapper = new YmtProductBrandWapper();
		int iBrandId = Integer.parseInt(productBean.get("iBrandId").toString());
		
		Map<String, Object> ymtProductBrand = ymtProductBrandWapper.getProductBrandByBrandId(iBrandId).get(0);
		String sBrand = "null";
		String sBrandEn = "null";
		if (!"null".equals(ymtProductBrand.get("sBrand"))) {
			sBrand = (String) ymtProductBrand.get("sBrand");
		}
		
		if (!"null".equals(ymtProductBrand.get("sBrandEn"))) {
			sBrandEn = (String) ymtProductBrand.get("sBrandEn");
		}
		
		//ProductsInLive sellerstatus
		YmtProductsInLiveWapper ymtProductsInLiveWapper  = new YmtProductsInLiveWapper();
		Map<String, Object> ymtProductsInLive = ymtProductsInLiveWapper.selectInLiveByProductId(productId).get(0);
		int iSellerStatus = (int) ymtProductsInLive.get("iSellStatus");
		
		
		////////////////////////////////////////////////////////////////////////
		Map<String, Object> liveProductInfoMap = new LiveProductInfoWapper().getliveproductByLiveId(liveId);
		
		Map<String, Object> tgtMap = new HashMap<String,Object>();
		
		tgtMap.put("ProductId", productId);
		tgtMap.put("LiveId", liveId);
		tgtMap.put("LiveName", liveBean.get("ActivityName"));
		tgtMap.put("SellerId", liveBean.get("UserId"));
		tgtMap.put("BrandId", productBean.get("iBrandId"));
		tgtMap.put("Brand", sBrand);
		tgtMap.put("BrandEN", sBrandEn);
		tgtMap.put("MasterCategoryId", MasterCategoryId);
		tgtMap.put("MasterCategoryName", MasterCategoryName);
		tgtMap.put("CategoryId", CategoryId);
		tgtMap.put("CategoryName", CategoryName); 
		tgtMap.put("ThirdCategoryId", ThirdCategoryId);
		tgtMap.put("ThirdCategoryName", ThirdCategoryName);
		tgtMap.put("SellStatus", iSellerStatus);
		tgtMap.put("IsTop", false);
		
		AssertService.assertResultEqual(tgtMap, liveProductInfoMap);
		
		//comment字段不维护		
		
	}
	
	
	
	
	/**
	 * JSONObject liveRqBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
	 * JSONObject productRqBean=SellerProductCallService.addNewProductOnSale(sellerId,sellerName,map); 
	 * 
	 * 添加商品到直播 - 检查【老Mongo库】【LiveBasicInfo】
	 * @param liveRqBean 直播请求+返回报文
	 * @param productRqBean 发布商品请求+返回报文
	 * @param liveId 直播id	
	 * @throws Exception
	 */
	public static void checkLiveBasicInfo(JSONObject liveRqBean,JSONObject productRqBean,int liveId) throws Exception{
		
		JSONObject bean = liveRqBean.getJSONObject("JsonBeanRequest").getJSONObject("ActivityRequest");
		
		Map<String, Object> mongoData = new LiveBasicInfoWapper().getActivityId(liveId);
		
		Map<String, String> tgtAndActKeyPaths = new HashMap<String, String>();
		
		//AppProductRelease..Ymt_Activity bAppConfirmed 直播是否定位
		boolean bAppConfirmed = (boolean) new YmtC2CLiveActivityWapper().selectLiveActivityByiActivityId(liveId).get(0).get("bAppConfirmed");
		
		tgtAndActKeyPaths.put("ActivityName", "ActivityName");		
		tgtAndActKeyPaths.put("Title", "Title");
		tgtAndActKeyPaths.put("VideoCover", "VideoCover");
		tgtAndActKeyPaths.put("VideoUrl", "VideoUrl");
		tgtAndActKeyPaths.put("UserId", "SellerId");
		tgtAndActKeyPaths.put("PicUrl", "ActivityPicture");
		tgtAndActKeyPaths.put("SellerName", "Seller");
		if (bAppConfirmed == true ) {
			tgtAndActKeyPaths.put("Position", "ShopAddress");
		}

		
		AssertService.assertResultEqual(tgtAndActKeyPaths, bean,mongoData);
	
		//check categroylist and brands
		Map<String, Object>  tgtMap = new HashMap<String,Object>();
		if (productRqBean==null){
			tgtMap.put("Brands", null);
			tgtMap.put("CategoryList", null);
			AssertService.assertResultEqual(tgtMap, mongoData);
		}else{
			
			JSONObject productBean = productRqBean.getJSONObject("JsonBeanRequest").getJSONObject("Product");
			JSONObject mongoDataMap = new JSONObject(mongoData);
			String brandName = new YmtProductBrandWapper().getProductBrandByBrandId(productBean.getInt("iBrandId")).get(0).get("sBrandEn").toString();
			
			YmtProductCategoryWapper ymtProductCategoryWapper = new YmtProductCategoryWapper();
			int ThirdCategoryId = Integer.parseInt(productBean.get("iThirdCategoryId").toString());
			Map<String, Object> ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(ThirdCategoryId).get(0);
			String ThirdCategoryName = ret.get("sCategory").toString();
			
			Logger.verifyEquals(true, mongoDataMap.get("Brands").toString().contains(brandName), "LiveBasicInfo-Brand");
			Logger.verifyEquals(true, mongoDataMap.get("CategoryList").toString().contains(String.valueOf(ThirdCategoryId)), "LiveBasicInfo-CategoryId");
			Logger.verifyEquals(true, mongoDataMap.get("CategoryList").toString().contains(ThirdCategoryName), "LiveBasicInfo-CategoryName");
		}
		
		
		
		
		//check country
		List<AppCountry> AppCountryList = new AppCountryWapper().selectByCountryId(bean.getInt("CountryId"));
		Logger.verifyEquals(1, AppCountryList.size(), "AppCountryList-count");
		Logger.verifyEquals(AppCountryList.get(0).getScountrynamezh(),mongoData.get("Country").toString(), "LiveBasicInfo-country");
		Logger.verifyEquals(AppCountryList.get(0).getSflag(),mongoData.get("Flag").toString(), "LiveBasicInfo-Flag");
		
		//check AppConfirm
		boolean tgtConfirm = bean.getInt("AppConfirmed")==1?true:false;
		Logger.verifyEquals(tgtConfirm, Boolean.parseBoolean(mongoData.get("AppConfirmed").toString()), "LiveBasicInfo-AppConfirmed");
		
		//check other value
		Logger.verifyEquals(1, Integer.parseInt(mongoData.get("Action").toString()), "LiveBasicInfo-Action");
		Logger.verifyEquals(liveId, Integer.parseInt(mongoData.get("ActivityId").toString()), "LiveBasicInfo-liveId");
		
		//check time
		AssertService.assertTime(YMTDateUtil.getDate(), (Date)mongoData.get("AddTime"), 1000);
		
		
		if (YMTDateUtil.getDate().getTime()>((Date)mongoData.get("StartTime")).getTime()){
			//表明正在进行直播
			Calendar c = Calendar.getInstance();
			c.add(Calendar.HOUR,-2);
			AssertService.assertTime(c.getTime(), (Date)mongoData.get("StartTime"), 1000);
			c.add(Calendar.HOUR,4);
			AssertService.assertTime(c.getTime(), (Date)mongoData.get("EndTime"), 1000);
			
		}else{
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(bean.getString("StartTime")), (Date)mongoData.get("StartTime"), 1000);
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(bean.getString("EndTime")), (Date)mongoData.get("EndTime"), 1000);
		}
	
	}
	
	
	
	
	
	
	
	/**
	 * JSONObject liveRqBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
	 * JSONObject productRqBean=SellerProductCallService.addNewProductOnSale(sellerId,sellerName,map); 
	 * 
	 * 添加商品到直播 - 检查新库LiveProductModel
	 * @param liveRqBean 直播请求+返回报文
	 * @param productRqBean 发布商品请求+返回报文
	 * @param liveId 直播id	
	 * @param productId 商品id
	 * @throws Exception
	 */
	public static void checkLiveProductModel(JSONObject liveRqBean,JSONObject productRqBean,int liveId,String productId) throws Exception{
		JSONObject liveBean = liveRqBean.getJSONObject("JsonBeanRequest").getJSONObject("ActivityRequest");
		JSONObject productBean = productRqBean.getJSONObject("JsonBeanRequest").getJSONObject("Product");
		
		
		//category data
		YmtProductCategoryWapper ymtProductCategoryWapper = new YmtProductCategoryWapper();
		
		int ThirdCategoryId = Integer.parseInt(productBean.get("iThirdCategoryId").toString());
		
		Map<String, Object> ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(ThirdCategoryId).get(0);
		String ThirdCategoryName = ret.get("sCategory").toString();
		int CategoryId = Integer.parseInt(ret.get("iMasterCategory").toString());
		
		ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(CategoryId).get(0);
		String CategoryName = ret.get("sCategory").toString();
		int MasterCategoryId = Integer.parseInt(ret.get("iMasterCategory").toString());
		
		ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(MasterCategoryId).get(0);
		String MasterCategoryName = ret.get("sCategory").toString();
		
		
		//Brand
		YmtProductBrandWapper ymtProductBrandWapper = new YmtProductBrandWapper();
		int iBrandId = Integer.parseInt(productBean.get("iBrandId").toString());
		
		Map<String, Object> ymtProductBrand = ymtProductBrandWapper.getProductBrandByBrandId(iBrandId).get(0);
		String sBrand = "null";
		String sBrandEn = "null";
		if (!"null".equals(ymtProductBrand.get("sBrand"))) {
			sBrand = (String) ymtProductBrand.get("sBrand");
		}
		
		if (!"null".equals(ymtProductBrand.get("sBrandEn"))) {
			sBrandEn = (String) ymtProductBrand.get("sBrandEn");
		}
		
		
		//ProductsInLive sellerstatus
		YmtProductsInLiveWapper ymtProductsInLiveWapper  = new YmtProductsInLiveWapper();
		Map<String, Object> ymtProductsInLive = ymtProductsInLiveWapper.selectInLiveByProductId(productId).get(0);
		int iSellerStatus = (int) ymtProductsInLive.get("iSellStatus");
		
		
		////////////////////////////////////////////////////////////////////////
		Map<String, Object> liveProductModelMap = new LiveProductModelWapper().getActivityId(liveId);
		
		Map<String, Object> tgtMap = new HashMap<String,Object>();
		
		tgtMap.put("ProductId", productId);
		tgtMap.put("LiveId", liveId);
		tgtMap.put("LiveName", liveBean.get("ActivityName"));
		tgtMap.put("SellerId", liveBean.get("UserId"));
		tgtMap.put("BrandId", productBean.get("iBrandId"));
		tgtMap.put("BrandName", sBrand);
		tgtMap.put("BrandEnName", sBrandEn);
		tgtMap.put("MasterCategoryId", MasterCategoryId);
		tgtMap.put("MasterCategoryName", MasterCategoryName);
		tgtMap.put("CategoryId", CategoryId);
		tgtMap.put("CategoryName", CategoryName); 
		tgtMap.put("ThirdCategoryId", ThirdCategoryId);
		tgtMap.put("ThirdCategoryName", ThirdCategoryName);
		tgtMap.put("SellStatus", iSellerStatus);
		tgtMap.put("IsTop", false);
		
		AssertService.assertResultEqual(tgtMap, liveProductModelMap);
		
		//comment字段不维护		
		
	}
	
	
	
	/**
	 * JSONObject liveRqBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
	 * JSONObject productRqBean=SellerProductCallService.addNewProductOnSale(sellerId,sellerName,map); 
	 * 
	 * 添加商品到直播 - 检查新库LiveModel
	 * @param liveRqBean 直播请求+返回报文
	 * @param productRqBean 发布商品请求+返回报文
	 * @param liveId 直播id	
	 * @throws Exception
	 */
	public static void checkLiveModel(JSONObject liveRqBean,JSONObject productRqBean,int liveId) throws Exception{
		
		JSONObject bean = liveRqBean.getJSONObject("JsonBeanRequest").getJSONObject("ActivityRequest");
		
		Map<String, Object> mongoData = new LiveModelWapper().getActivityId(liveId);
		
		Map<String, String> tgtAndActKeyPaths = new HashMap<String, String>();
		
		//AppProductRelease..Ymt_Activity bAppConfirmed 直播是否定位
		boolean bAppConfirmed = (boolean) new YmtC2CLiveActivityWapper().selectLiveActivityByiActivityId(liveId).get(0).get("bAppConfirmed");
		
		tgtAndActKeyPaths.put("ActivityName", "ActivityName");		
		tgtAndActKeyPaths.put("Title", "Title");
		tgtAndActKeyPaths.put("VideoCover", "VideoCover");
		tgtAndActKeyPaths.put("VideoUrl", "VideoUrl");
		tgtAndActKeyPaths.put("UserId", "SellerId");
		tgtAndActKeyPaths.put("PicUrl", "ActivityPicture");
		tgtAndActKeyPaths.put("SellerName", "Seller");
		if (bAppConfirmed == true ) {
			tgtAndActKeyPaths.put("Position", "ShopAddress");
		}
	
		
		AssertService.assertResultEqual(tgtAndActKeyPaths, bean,mongoData);
	
		//check categroylist and brands
		Map<String, Object>  tgtMap = new HashMap<String,Object>();
		if (productRqBean==null){
			tgtMap.put("Brands", null);
			tgtMap.put("CategoryList", null);
			AssertService.assertResultEqual(tgtMap, mongoData);
		}else{
			
			JSONObject productBean = productRqBean.getJSONObject("JsonBeanRequest").getJSONObject("Product");
			JSONObject mongoDataMap = new JSONObject(mongoData);
			
			YmtProductBrandWapper ymtProductBrandWapper = new YmtProductBrandWapper();
			
			
			//===============================
			String productId = productRqBean.getJSONObject("Data").getString("ProductID");
			String brandName = null;
			String brandInProduct = new YmtProductsIWapper().selectProductByProductId(productId).get(0).getsBrand();
			if (brandInProduct!=null&&!brandInProduct.equals("")){
				brandName = brandInProduct;
			}
			else{
				if ("".equals(ymtProductBrandWapper.getProductBrandByBrandId(productBean.getInt("iBrandId")).get(0).get("sBrand").toString()) || ymtProductBrandWapper.getProductBrandByBrandId(productBean.getInt("iBrandId")).get(0).get("sBrand").toString() ==null) {
					brandName = ymtProductBrandWapper.getProductBrandByBrandId(productBean.getInt("iBrandId")).get(0).get("sBrandEn").toString();
				}else {
					brandName = ymtProductBrandWapper.getProductBrandByBrandId(productBean.getInt("iBrandId")).get(0).get("sBrand").toString();
				}		
			}
			
			//===============================
			
			YmtProductCategoryWapper ymtProductCategoryWapper = new YmtProductCategoryWapper();
			int ThirdCategoryId = Integer.parseInt(productBean.get("iThirdCategoryId").toString());
			Map<String, Object> ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(ThirdCategoryId).get(0);
			String ThirdCategoryName = ret.get("sCategory").toString();
			
			Logger.verifyEquals(true, mongoDataMap.get("Brands").toString().contains(brandName), "LiveModel-Brand");
			Logger.verifyEquals(true, mongoDataMap.get("CategoryList").toString().contains(String.valueOf(ThirdCategoryId)), "LiveModel-CategoryId");
			Logger.verifyEquals(true, mongoDataMap.get("CategoryList").toString().contains(ThirdCategoryName), "LiveModel-CategoryName");
		}
		
		
		
		
		//check country
		List<AppCountry> AppCountryList = new AppCountryWapper().selectByCountryId(bean.getInt("CountryId"));
		Logger.verifyEquals(1, AppCountryList.size(), "AppCountryList-count");
		Logger.verifyEquals(AppCountryList.get(0).getScountrynamezh(),mongoData.get("Country").toString(), "LiveModel-country");
		Logger.verifyEquals(AppCountryList.get(0).getSflag(),mongoData.get("Flag").toString(), "LiveModel-Flag");
		
		//check AppConfirm
		boolean tgtConfirm = bean.getInt("AppConfirmed")==1?true:false;
		Logger.verifyEquals(tgtConfirm, Boolean.parseBoolean(mongoData.get("AppConfirmed").toString()), "LiveModel-AppConfirmed");
		
		//check other value
		Logger.verifyEquals(1, Integer.parseInt(mongoData.get("Action").toString()), "LiveModel-Action");
		Logger.verifyEquals(liveId, Integer.parseInt(mongoData.get("ActivityId").toString()), "LiveModel-liveId");
		
		//check time
		AssertService.assertTime(YMTDateUtil.getDate(), (Date)mongoData.get("AddTime"), 1000);
		
		
		if (YMTDateUtil.getDate().getTime()>((Date)mongoData.get("StartTime")).getTime()){
			//表明正在进行直播
			Calendar c = Calendar.getInstance();
			c.add(Calendar.HOUR,-2);
			AssertService.assertTime(c.getTime(), (Date)mongoData.get("StartTime"), 1000);
			c.add(Calendar.HOUR,4);
			AssertService.assertTime(c.getTime(), (Date)mongoData.get("EndTime"), 1000);
			
		}else{
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(bean.getString("StartTime")), (Date)mongoData.get("StartTime"), 1000);
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(bean.getString("EndTime")), (Date)mongoData.get("EndTime"), 1000);
		}
	
	}
	
	
	
	
	
	
	
}
